$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/cash/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true},
            { label: '提现单号', name: 'cashnum', index: 'cashNum', width: 80 },
            { label: '账号', name: 'account', index: 'account', width: 80 },
            { label: '收款银行', name: 'bank', index: 'bank', width: 80 },
            { label: '提现金额', name: 'money', index: 'money', width: 80,formatter: function(cellvalue, options, rowdata) {
                    return "￥"+rowdata.money;


                } },
            { label: '提现时间', name: 'createtime', index: 'createtime', width: 80 },
			{ label: '提现人', name: 'userid', index: 'userId', width: 80,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.userid!=null&&rowdata.userid!=""){
                        var username = "";

                        $.ajax({
                            type: "POST",
                            async:false,
                            url: baseURL + "sys/user/info/"+rowdata.userid,
                            success: function(r){
                                username =r.user.username;


                            }
                        });

                        return username;
                    }else{
                        return '';
                    }


                } },
			{ label: '状态', name: 'statu', index: 'statu', width: 80,formatter: function(item, index) {
                    if (item === 0) {
                        return '等待审核';
                    }
                    if (item === 1) {
                        return '审核通过';
                    }
                    if (item === 2) {
                        return '审核未通过';
                    }
                }  },
			{ label: '操作', name: 'sy', width: 90,formatter: function(cellvalue, options, rowdata) {
					if(rowdata.statu == 0){
                        return "<a style='cursor:pointer;' onclick='vm.showexamine("+ rowdata.id +")'>审核</a>" +
							"&nbsp;&nbsp;&nbsp;&nbsp;<a style='cursor:pointer;' onclick='vm.getDetails("+ rowdata.id +")'>详情</a>";
					}else{
						return "<a style='cursor:pointer;' onclick='vm.getDetails("+ rowdata.id +")'>详情</a>";
					}


                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			number:null,
			statu:null
		},
		showList: true,
        showLists: true,
        showListInfo: true,
		title: null,
		cash: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
            vm.showList = false;
			vm.showLists = false;
			vm.title = "新增";
			vm.cash = {};
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/user/info",
                success: function(r){
					$("#balance").text("￥"+r.user.balance);

                }
            });
		},

		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
        showexamine: function (id) {
            vm.cash = {};
            vm.getInfo(id);
            $('#examineModal').modal();
        },

        sh: function (event) {
			vm.cash.statu = $("#statu").val();
			vm.cash.remark = $("#remark").val();
			if(vm.cash.statu == 0){
				alert("请选择审核状态");
			}else{
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/cash/update",
                    contentType: "application/json",
                    data: JSON.stringify(vm.cash),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
			}

        },
        getDetails: function (id) {
            vm.showListInfo = false;
            vm.showList = false;
            vm.getInfo(id);
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/user/info",
                success: function(r){
                    $("#balances").text("￥"+r.user.balance);

                }
            });
            $("#money").text("￥"+vm.cash.money);
            $("#bank").text(vm.cash.bank);
            $("#account").text(vm.cash.account);
            var s = "";
            if (vm.cash.statu === 0) {
                s = '等待审核';
            }
            if (vm.cash.statu === 1) {
                s = '审核通过';
            }
            if (vm.cash.statu === 2) {
                s =  '审核未通过';
            }
            $("#status").text(s);
            $("#remarks").text(vm.cash.remark);
        },
		saveOrUpdate: function (event) {
			var url = vm.cash.id == null ? "sys/cash/save" : "sys/cash/update";
        	if($("#balance").val()<vm.cash.money){
                alert("提现金额不能大于可用余额");
            }else{
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.cash),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
			}

		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/cash/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/cash/info/"+id,
                success: function(r){
                    vm.cash = r.cash;

                }
            });
		},
		reload: function (event) {
            var startime = $("#startime").val();
            var endtime = $("#endtime").val();
            if((startime!=''&&typeof startime!='undefined')&&(endtime!=''&&typeof endtime!='undefined')){
                var st = new Date(startime).getTime();
                var et = new Date(endtime).getTime();
                if(st>et){
                    alert("开始时间不能大于结束时间");
                    return;
                }
            }
			vm.showList = true;
			vm.showListInfo = true;
			vm.showLists = true;
            if(endtime!=''&&typeof endtime!='undefined'){
                var date = new Date(new Date(endtime).getTime()+(24*60*60*1000));
                var premonth   = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
                if(premonth<10){
                    premonth = "0"+premonth;
                }
                var preday = date.getDate(); //获取当前日(1-31)
                if(preday<10){
                    preday = "0"+preday;
                }
                endtime = date.getFullYear()+"-"+premonth+"-"+preday;
            }
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'number':vm.q.number,'statu':vm.q.statu,'startime':startime,'endtime':endtime},
                page:page
            }).trigger("reloadGrid");
		}
	}
});